package com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.mvp_lib.View.BaseFragment;

import com.bw.liuyushan.zhuangao4_weektest2.DaoSQL.UserMsg;
import com.bw.liuyushan.zhuangao4_weektest2.DaoSQL.UserMsgUtils;
import com.bw.liuyushan.zhuangao4_weektest2.Entity.UserBean;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Activity.MainActivity;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_weektest2.R;

/**
 * ZhuanGao4_WeekTest2
 * file name is : Login_Fragment
 * created by Ender on 2021/8/23 9:46.
 * author : 刘雨杉
 * Describe(作用):
 */
public class Login_Fragment extends BaseFragment<BasePresenterTest> implements IContrackTest.C_ViewTest {

    private EditText name2;
    private EditText pass2;
    private Button loginin;
    private Button nologin;
    String username;
    String password;

    @Override
    public int bindLayout() {
        return R.layout.login_fragment;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip = new BasePresenterTest(new BaseModelTest(), this);

        name2 = (EditText) findViewById(R.id.name2);
        pass2 = (EditText) findViewById(R.id.pass2);
        loginin = (Button) findViewById(R.id.loginin);
        nologin = (Button) findViewById(R.id.nologin);
    }

    @Override
    public void initDate() {
        nologin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("别忘记登录哦~登录可体验更多精彩");
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
        loginin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = name2.getText().toString();
                password = pass2.getText().toString();
                if (username.isEmpty() && password.isEmpty()) {
                    ToastUtils.showShort("账户或者是密码不能为空");
                } else {
                    if (password.isEmpty()) {
                        ToastUtils.showShort("账户或者是密码不能为空");
                    } else {
                        ip.mpLogin(username, password);
                    }
                }
            }
        });
    }

    @Override
    public void ShowLogin(UserBean userBean) {
        if (userBean.getErrorCode() == 0) {
            ToastUtils.showShort("登陆成功,欢迎你");
            SharedPreferences sp = getActivity().getSharedPreferences("sp", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putBoolean("isLogin",true);
            edit.putString("namesp",username);//发送登录用户的用户名 确认当前登录用户
            edit.commit();

            int size = UserMsgUtils.getInstance().getDaoSession().queryBuilder().list().size();
            ToastUtils.showShort("数据库目前有"+size);
            Intent intent = new Intent(getActivity(), MainActivity.class);
//            intent.putExtra("name",username);
            startActivity(intent);
        } else {
            ToastUtils.showShort("登录失败,信息有误");
        }
    }
}
